1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#![allow(non_camel_case_types, non_snake_case)]

use crate::co;
use crate::decl::*;
use crate::kernel::privs::*;
use crate::prelude::*;
use crate::shell::ffi;

impl shell_Hwnd for HWND {}

/// This trait is enabled with the `shell` feature, and provides methods for
/// [`HWND`](crate::HWND).
///
/// Prefer importing this trait through the prelude:
///
/// ```no_run
/// use winsafe::prelude::*;
/// ```
pub trait shell_Hwnd: ole_Hwnd {
	/// [`DragAcceptFiles`](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-dragacceptfiles)
	/// function.
	fn DragAcceptFiles(&self, accept: bool) {
		unsafe { ffi::DragAcceptFiles(self.ptr(), accept as _); }
	}

	/// [`ShellAbout`](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellaboutw)
	/// function.
	fn ShellAbout(&self,
		title_bar: &str,
		first_line: Option<&str>,
		other_stuff: Option<&str>,
		hicon: Option<&HICON>,
	) -> SysResult<()>
	{
		bool_to_sysresult(
			unsafe {
				ffi::ShellAboutW(
					self.ptr(),
					WString::from_str(
						&match first_line {
							Some(line) => format!("{}#{}", title_bar, line),
							None => title_bar.to_owned(),
						},
					).as_ptr(),
					WString::from_opt_str(other_stuff).as_ptr(),
					hicon.map_or(std::ptr::null_mut(), |h| h.ptr()),
				)
			},
		)
	}

	/// [`ShellExecute`](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew)
	/// function.
	fn ShellExecute(&self,
		operation: &str,
		file: &str,
		parameters: Option<&str>,
		directory: Option<&str>,
		show_cmd: co::SW,
	) -> SysResult<()>
	{
		let ret = unsafe {
			ffi::ShellExecuteW(
				self.ptr(),
				WString::from_str(operation).as_ptr(),
				WString::from_str(file).as_ptr(),
				parameters.map_or(std::ptr::null(), |lp| WString::from_str(lp).as_ptr()),
				directory.map_or(std::ptr::null(), |lp| WString::from_str(lp).as_ptr()),
				show_cmd.raw(),
			)
		};

		if ret as usize > 32 {
			Ok(())
		} else {
			Err(GetLastError())
		}
	}
}